#! /bin/bash

#### BAK bash script
#### Deletes backup files
#### v4 05/2010 Simone Cingano

#
hflag=no # help
nflag=no # nodelete - do not delete files
rflag=no # recursion
pflag=no # pyc - consider .pyc file
vflag=no # list files (performs delete anyway)
aflag=no # all - flags r + p + m
mflag=no # vim - consider .swp, .swo, swn
dflag=no # specify directory
#
directory=""
startdir=$PWD
TMP=bak`date +%s`
#
esc_bold='\033[1m' # http://en.wikipedia.org/wiki/ANSI_escape_code#CSI_Codes
esc_reset='\033[0m'
esc_red='\033[91m' 
esc_green='\033[92m'
#

####################################################################################
# GETOPT

set -- $(getopt hnmrpva "$@")
while [ $# -gt 0 ]
do
    case "$1" in
    -h) hflag=yes;;
    -n) nflag=yes;;
    -r) rflag=yes;;
    -p) pflag=yes;;
    -m) mflag=yes;;
    -v) vflag=yes;;
    -a) pflag=yes; mflag=yes; rflag=yes;;
    --) shift; break;;
    -*) echo "$0: error - unrecognized option $1" 1>&2; exit 1;;
    *)  break;;
    esac
    shift
done

####################################################################################
# HELP FLAG
# print help and exit

if [ $hflag = yes ]; then
    echo
	echo -e "${esc_bold}Usage:${esc_reset} bak [-hnrpva] [DIRECTORY]"
    echo -e "  Deletes backup files ( *~ )"
    echo -e "  If no DIRECTORY deletes in current directory"
    echo
    echo -e "${esc_bold}Options:${esc_reset}"
    echo -e "   ${esc_green}-h${esc_reset}     Display this help"
    echo -e "   ${esc_green}-n${esc_reset}     Dry run (${esc_green}no delete will occur${esc_reset})"
    echo -e "   ${esc_green}-r${esc_reset}     Recursive"
    echo -e "   ${esc_green}-p${esc_reset}     Deletes also .pyc"
    echo -e "   ${esc_green}-m${esc_reset}     Deletes also VIM files (${esc_red}backup & swap${esc_reset})"
    echo -e "   ${esc_green}-v${esc_reset}     Verbose (lists files)"
    echo -e "   ${esc_green}-a${esc_reset}     Alias for -rpm"
	echo
    exit 0
fi

####################################################################################
# PREPARE

# Directory
directory=${1:-$PWD}
# check if given directory is a directory
if [ ! -d $directory ] ; then
    echo "'$directory' is not a directory"
    exit 1
fi

# Recursion
depth='-maxdepth 1'
andits=''
if [ $rflag = yes ]; then
    depth='' #recursive
    andits=' and its subdirectories'
fi

####################################################################################
# EXEC

cd $directory

#list files in TMP file
find . -name "*~" -type f $depth -exec echo "\"{}\"" \; > /tmp/$TMP 2> /dev/null
if [ $pflag = yes ]; then
    find . -name \*.pyc -type f $depth -exec echo "\"{}\"" \; >> /tmp/$TMP 2> /dev/null
fi
if [ $mflag = yes ]; then
    find . \( -name \*.swp -or -name \*.swo -or -name \*.swn \) $depth -exec echo "\"{}\"" \; >> /tmp/$TMP 2> /dev/null
fi

FILES=`wc -l /tmp/$TMP | cut -d/ -f1 | sed 's/[ ]//g' `
if [ ${FILES} -ge 1 ] ; then

    if [ $vflag = yes ]; then
        # list files
        cat -n /tmp/$TMP | sed 's/"//g'
        echo
    fi
    if [ ${FILES} = 1 ]; then
        label_file="file"
    else
        label_file="files"
    fi

    if [ $nflag = no ]; then
        cat /tmp/$TMP | xargs rm -f #delete
        echo -e "  ${esc_red}$FILES bak ${label_file}${esc_reset} deleted in ${esc_green}$directory${esc_reset}$andits"
    else
        echo -e "  ${esc_green}$FILES bak ${label_file}${esc_reset} listed in ${esc_green}$directory${esc_reset}$andits"
    fi

else
    echo -e "  ${esc_green}No bak files${esc_reset} listed in ${esc_green}$directory${esc_reset}$andits"
fi

#remove TMP file
rm -f /tmp/$TMP

# return to startdir
cd $startdir

exit 0
